import flet as ft

from dataTemp.user import Role

jobs = ["狂战士", "剑魂", "鬼泣", "阿修罗", "漫游", "枪炮师", "散打", "气功师", "元素师", "魔道", "奶妈"]


class Role_ui(ft.Container):
    def __init__(self, page, roles, role):
        super().__init__()
        self.border_radius = 10
        self.bgcolor = ft.colors.CYAN_100
        # print(f"初始化角色{role}")
        self.content = ft.Row([
            ft.Container(
                content=ft.Image(src="../tasks/headIco/roles/" + role.job + ".jpg"),
                width=40,
                height=40,
                border_radius=20,  # 半径设置为一半，以形成圆形
                clip_behavior=ft.ClipBehavior.ANTI_ALIAS  # 确保裁剪为圆形
            ),
            ft.Container(content=ft.Row([
                ft.Column([ft.Text('职业'), ft.Text(role.job)], spacing=5,
                          horizontal_alignment=ft.CrossAxisAlignment.CENTER),
                ft.Column([ft.Text('名字'), ft.Text(role.name)], spacing=5,
                          horizontal_alignment=ft.CrossAxisAlignment.CENTER),
                ft.Column([ft.Text('位置'), ft.Text(role.index)], spacing=5,
                          horizontal_alignment=ft.CrossAxisAlignment.CENTER),
            ], spacing=12), padding=ft.padding.only(top=3)),
            ft.Column([
                ft.TextButton("修改", height=25, on_click=lambda e: reiseRole(page, roles, role)),
                ft.TextButton("删除", height=25)
            ], spacing=0, run_spacing=0)
        ], spacing=0, alignment=ft.MainAxisAlignment.SPACE_BETWEEN)


def addRole(page: ft.Page, roles,callback):
    local_job = ft.Dropdown(width=80, options=[ft.dropdown.Option(i) for i in jobs])
    local_job.value="狂战士"
    local_index = ft.Dropdown(width=80, options=[ft.dropdown.Option(i) for i in range(1, 6)])
    local_index.value = "1"
    local_name = ft.TextField()
    # page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    dlg = ft.AlertDialog(
        title=ft.Text("添加角色"),
        on_dismiss=lambda e: page.add(ft.Text("Non-modal dialog dismissed")),
        content=ft.Container(content=ft.Column([
            ft.Container(content=ft.Column([
                ft.Image(src="../tasks/headIco/cc.jpg", width=600),
                ft.Container(content=ft.Text("说明"), height=100, width=600,
                             border=ft.Border(left=ft.BorderSide(width=1),
                                              top=ft.BorderSide(width=1),
                                              right=ft.BorderSide(width=1),
                                              bottom=ft.BorderSide(width=1)))
            ])),
            ft.Divider(),  # 添加一条水平线
            ft.Row([
                ft.Text("职业"),
                local_job,
                ft.Text("名字"),
                local_name,
                ft.Text("位置"),
                local_index,
            ]),
            ft.ElevatedButton("确定", on_click=lambda e: button_clicked(e)),

        ], spacing=10, horizontal_alignment=ft.CrossAxisAlignment.CENTER))
    )
    page.open(dlg)

    def button_clicked(e):
        # print( f"Dropdown 值是： {local_job.value}")
        # print(f"Dropdown 值是： {local_index.value}")
        roles.append(Role(int(local_index.value),local_job.value))
        page.close(dlg)
        page.update()
        callback()
        print(f"Dropdown 值是： {len(roles)}")

    # page.add(ft.ElevatedButton("Open dialog", on_click=lambda e: page.open(dlg)))


def reiseRole(page: ft.Page, roles, currRole):
    local_job = ft.Dropdown(width=80, options=[ft.dropdown.Option(i) for i in jobs])
    local_job.value = currRole.job
    local_index = ft.Dropdown(width=80, options=[ft.dropdown.Option(i) for i in range(1, 6)])
    local_index.value = currRole.index
    local_name = ft.TextField()
    local_name.value = currRole.name
    # page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    dlg = ft.AlertDialog(
        # title=ft.Text("添加角色"),
        on_dismiss=lambda e: page.add(ft.Text("Non-modal dialog dismissed")),
        content=ft.Container(content=ft.Column([
            ft.Container(content=ft.Column([
                ft.Image(src="../tasks/headIco/cc.jpg", width=600),
                ft.Container(content=ft.Text("说明"), height=100, width=600,
                             border=ft.Border(left=ft.BorderSide(width=1),
                                              top=ft.BorderSide(width=1),
                                              right=ft.BorderSide(width=1),
                                              bottom=ft.BorderSide(width=1)))
            ])),
            ft.Divider(),  # 添加一条水平线
            ft.Row([
                ft.Text("职业"),
                local_job,
                ft.Text("名字"),
                local_name,
                ft.Text("位置"),
                local_index,
            ]),
            ft.ElevatedButton("确定", on_click=lambda e: button_clicked(e)),

        ], spacing=10, horizontal_alignment=ft.CrossAxisAlignment.CENTER))
    )
    page.open(dlg)

    def button_clicked(e):
        print(f"Dropdown 值是： {local_job.value}")
        print(f"Dropdown 值是： {local_index.value}")
        # roles.append(Role(local_job.value, local_name.value, local_index.value))
        # sorted(roles, key=lambda e: e.index)
        currRole.job = local_job.value
        currRole.index = local_index.value
        currRole.name = local_name.value
        print(f"排序后数组 值是： {currRole}")
        page.close(dlg)
        page.update()
    # page.add(ft.ElevatedButton("Open dialog", on_click=lambda e: page.open(dlg)))


if __name__ == '__main__':
    ft.app(target=addRole)
    # main_yl()
    pass
